Part Number Hot Search : 
TLE42 03N60S5 P6KE13 11SRWA 74HC27 C114T TIP58A DFREE
Product Description
Full Text Search
 

To Download AN1204 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  1/27 december 2000 AN1204 application note software drivers for the m58bf008 flash memory contents n introduction n the m58bf008 programming model n writing c code for the m58bf008 n c library functions provided n porting the drivers to the target system n limitations of the software n conclusion n c1204_32.h listing n c1204_32.c listing introduction this application note provides library source code in c for the m58bf008 flash memory. the m58bf008 supports both asynchronous and synchronous burst bus interfaces. the c code drivers work on a layer above the hardware and can be used successfully over either bus interface. listings of the source code can be found at the end of this doc- ument. the source code is also available in file form from the internet site http://www.st.com or from your stmicroelectronics distributor. the c1204_32.c and c1204_32.h files contain librar- ies for accessing the m58bf008 flash memories. also included in this application note is an overview of the pro- gramming model for the m58bf008. this will familiarize the reader with the operation of the memory devices and provide a basis for understanding and modifying the accompanying source code. the source code is written to be as platform independent as possible and requires minimal changes by the user in order to compile and run. the application note explains how the user should modify the source code for their individual target hard- ware. all of the source code is backed up by comments explain- ing how it is used and why it has been written as it has. this application note does not replace the m58bf008 datasheet. it refers to the datasheet throughout and it is neces- sary to have a copy in order to follow some of the explanations. the software and accompanying documentation has been fully tested on a target platform. it is small in size and can be applied to any target hardware. the m58bf008 programming model the m58bf008 is an 8mb (256kb x32) flash memory which can be electrically erased and programmed through special coded command sequences on most standard microprocessor buses. the device is broken down into 32 blocks, each 32 kbytes in size. each block can be erased individually, or the whole chip can be erased at once, erasing all 8mb. an addition- al 32 kbyte overlay block is provided to hold the boot code for the microprocessor; once booted the microprocessor can hide the overlay block and access block 0. the m58bf008 is a smart voltage device. it differs from first generation dual voltage devices which require a 12v supply to
AN1204 - application note 2/27 program or erase. the m58bf008 is therefore easier to use since the hardware does not need to cater for special bus signal levels. the voltages needed to erase the device are generated by charge pumps inside the device. three power supply pins are used to give the optimal supply voltage conditions. in normal op- eration the core is supplied with 5v, the i/o buffers are supplied with 3.3v and the v pp supply pin is sup- plied with 0v (all blcks protected) or 5v (no blocks protected). the program/erase section can be supplied with 12v through v pp pin to speed up programming in the factory; this mode is not to be used as a per- manent solution. included in the device is a program/erase controller. with first generation flash memory devices the soft- ware had to manually program all of the words to 0000h before erasing to ffffh using special program- ming sequences. the program/erase controller in the m58bf008 allows a simpler programming model to be used, by taking care of all the necessary steps required to erase and program the memory. this has led to improved reliability so that in excess of 10,000 program/erase cycles are guaranteed per block on the device. bus operations and commands most of the functionality of the m58bf008 is available via the two standard bus operations: read and write. read operations retrieve data or status information from the device. write operations are interpreted by the device as commands, which modify the data stored or the behavior of the device. only certain special sequences of write operations are recognized as commands by the m58bf008. the various commands recognized by the m58bf008 are listed in the instructions table of the datasheet; the main commands can be grouped as follows: 1. read 2. read electronic signature 3. erase 4. program 5. program/erase suspend 6. enable/disable overlay block 7. asynchronous/synchronous bus interface toggle. the read command returns the m58bf008 to read mode where it behaves as a rom. in this state, a read operation outputs onto the data bus the data stored at the specified address of the device. the read electronic signature command places the device in a mode which allows the user to read the manufacturer, device code and version code of the device. in the library of software drivers this mode is known as the auto select mode to make the m58bf008 compatible with the m29 series flash memories. the erase command is used to set all the bits to 1 in every memory location in the selected block. all data previously stored in the erased block will be lost. the erase command takes longer to execute than the other commands, because an entire block is erased at once. attempts to erase or program a block while the memory is protected (i.e. v pp = v il ) generate an error and do not modify the contents of the memory. a special command is used to erase the overlay block, providing additional security for the data in the overlay block. the program command is used to modify the data stored at the specified address of the device. note that programming can only change bits from 1 to 0. attempting to change a 0 to a 1 using the program command will fail, though the status register may not indicate the failure. it may therefore be necessary to erase the block before programming to addresses within it. programming modifies a single word at a time. programming larger amounts of data must be done one word at a time, by issuing a program com- mand, waiting for the command to complete, then issuing the next program command, and so on. a spe- cial command is used to program the overlay block, providing additional security for the data in the overlay block.
3/27 AN1204 - application note issuing the program/erase suspend command during a program or erase operation will temporarily place the m58bf008 in program/erase suspend mode. while an erase operation is being suspended the blocks not being erased may be read or programmed as if in the reset state of the device. while a program operation is being suspended the rest of the device may be read. this allows the user to access informa- tion stored in the device immediately rather than waiting until the program or erase operation completes, typically 10 m s for programming and 0.33s for erasing on the m58bf008. the program or erase operation is resumed when the device receives the program/erase resume command. the overlay block is read in the same address space as block 0. the overlay block toggle command is used to switch between reading the overlay block and reading block 0. special commands are used to program or erase the overlay block and are independent of the block selected for read mode. the bus interface on read operations can be toggled between asynchronous and synchronous modes. however write operations will be either asynchronous or synchronous depending on the factory configu- ration of the device. the c code does not include this function as it would typically be performed as part of the hardware initialisation. the status register while the m58bf008 is programming or erasing, a read from the device will output the status register of the program/erase controller. the status register, which can also be accessed by issuing the read sta- tus register command, provides valuable information about the most recent program or erase command. the status register bits are described in the status register bits table of the m58bf008 datasheet. their main use is to determine when programming or erasing is complete and whether it is successful or not. completion of the program or erase operation is indicated by the program/erase controller status bit (status register bit dq7) becoming 1. programming or erasing errors are then indicated by one or more of the various error bits (status register bits dq3, dq4 and dq5) being 1. if a failure occurs the status register error bits will remain set until a clear status register command is issued to the device. this should be done before performing any further operations or it will not be possible to determine whether the following operation resulted in an error or not. a detailed example the instructions table of the m58bf008 datasheet describes the sequences of write operations that will be recognized by the program/erase controller as valid commands. for example programming 3f819465h to the address 03e2h requires the user to write the following sequence (in c): *(unsigned long*)(0x0000) = 0x00000040; *(unsigned long*)(0x03e2) = 0x3f819465; the first of the two addresses (0000h) is arbitrary, so long as it is inside the flash address space. this example assumes that address 0000h of the m58bf008 is mapped to address 0000h in the microproces- sor address space. in practice it is likely that the flash will have a base offset which needs to be added to the address. while the device is programming the specified address, read operations will access the status register bits. status register bit dq7 will be 0 while programming is on-going and will become 1 on completion. if status register bits dq3 or dq4 are set on completion then the program command will have failed. the status register bits do not indicate an error when an attempt to change a 0 to a 1 has been made; it is recommended that the value in the memory after programming is compared to the desired value to trap this error. writing c code for the m58bf008 the low-level functions (drivers) described in this application note have been provided to simplify the pro- cess of developing application code in c for the stmicroelectronics flash memories (m58bf008). this enables users to concentrate on writing the high level functions required for their particular applications.
AN1204 - application note 4/27 these high level functions can access the flash memories by calling the low level drivers, hence keeping details of special command sequences away from the users' high level code: this will result in source code both simpler and easier to maintain. code developed using the drivers provided can be decomposed into three layers: 1. the hardware specific bus operations 2. the low-level drivers 3. the high level functions written by the user the implementation in c of the hardware specific read and write bus operations is required by the low- level drivers in order to communicate with the m58bf008. this implementation is hardware platform de- pendent as it is affected by which microprocessor the c code runs on and by where in the microproces- sor's address space the memory device is located. the user will have to write the c functions appropriate to his hardware platform (see flashread() and flashwrite() in the next section). the low-level drivers take care of issuing the correct sequences of write operations for each command and of interpreting the information received from the device during programming or erasing. these drivers encode all the specific details of how to issue commands and how to interpret the status register bits. the high level functions written by the user will access the memory device by calling the low-level func- tions. by keeping the specific details of how to access the m58bf008 away from the high level functions, the user is left with code which is simple and easier to maintain. it also makes the user's high level func- tions easier to apply to other stmicroelectronics flash memories. when developing an application, the user is advised to proceed as follows: C first write a simple program to test the low level drivers provided and verify that these operate as ex- pected on the user's target hardware and software environments. C then the user should write the high level code for his application, which will access the flash memories by calling the low level drivers provided. C finally test the complete application source code thoroughly. c library functions provided the software library provided with this application note provides the user with source code for the following functions: flashreadreset() is used to reset the device into the read array mode. note that there should be no need to call this function under normal operation as all of the other software library functions leave the device in this mode. flashautoselect() is used to identify the manufacturer code, device code and version code of the device. the function uses the read electronic signature mode of the device. the function is called flashautoselect() to make it compatible with the m29 series flash memories. flashblockerase() is used to erase a block in the device. the blocks cannot be erased when v pp is invalid: attempting to do so generates an error and leaves the flash in an indeterminate state. flashchiperase() is used to erase the entire chip. the chip cannot be erased when v pp is invalid: at- tempting to do so generates an error and leaves the flash in an indeterminate state. flashprogram() is used to program data arrays into the flash. only previously erased double words can be programmed reliably. again, programming cannot take place when v pp is invalid. flashoverlayblockenable() is used to enable the overlay block and disable block 0 for future calls to flashread() . flashoverlayblockdisable() is used to disable the overlay block and enable block 0 for future calls to flashread() .
5/27 AN1204 - application note flashoverlayblockread() is used to read a double word from the overlay block and then disable the overlay block. it is useful to retrieve single values, for multiple reads enable the overlay block, read the values required and disable it again. flashoverlayblockerase() is used to erase the overlay block. the overlay block cannot be erased when v pp is invalid: attempting to do so generates an error and leaves the flash in an indeterminate state. flashoverlayprogram() is used to program data arrays into the flash. only previously erased words can be programmed reliably. again, the overlay block cannot be programmed when v pp is invalid. the functions provided in the software library rely on the user implementing the hardware specific bus op- erations. this is to be done by writing two functions as follows: C flashread() must be written to read a value from the flash. C flashwrite() must be written to write a value to the flash. an example of these functions is provided in the source code. in many instances these functions can be written as macros and therefore will not incur the function call time overhead. the two functions which perform the basic i/o to the device have been provided for users who have awkward systems. for example where the addressing system is peculiar or the data bus has d0..d15 of the device on d16..d31 of the microprocessor. they allow any user to quickly adapt the code to virtually any target system. throughout the functions assumptions have been made on the data types. these are: a char is 8 bits (1 byte). this is not the case in all microcontrollers. where it is not it will be necessary to mask the unused bits of the word. an int is 16 bits (2 bytes). again, like the char , if this is not the case it will be necessary to use a variable type which is 16 bits or longer and mask bits above 16 bits (particularly in the users flashread() func- tion). a long is 32 bits (4 bytes). it is necessary to have arithmetic greater than 16 bits in order to address the entire device. two approaches to the addressing are available: the desired address in the flash can be specified by a 32 bit linear pointer or a 32 bit offset into the device could be provided by the user. the flashread() functions in each case would declared as: unsigned long flashread( unsigned long *addr); unsigned long flashread( unsigned long uloff); the pointer option has the advantage that it runs faster. the 32 bit offset needs to be changed to an ad- dress for each access and this involves 32 bit arithmetic. using a 32 bit offset is, however, more portable since the resulting software can easily be changed to run on microprocessors with segmented memory spaces (such as the 8086). for maximum portability all the functions in this application note use a 32 bit unsigned long offset, rather than a pointer. porting the drivers to the target system before using the software in the target system the user needs to write flashread() and flash- write() functions appropriate to the target hardware. the example flashread() and flash- write() functions provided in the source code should give the user a good idea of what is required and can be used in many instances without much modification. to test the source code in the target system start by simply reading from the m58bf008. if it is erased then only ffffffffh data should be read. next read the manufacturer and device codes and check they are correct. if these functions work then it is likely that all of the functions will work but they should all be tested thoroughly.
AN1204 - application note 6/27 the programmer needs to take extra care when the device is accessed during an interrupt service routine. two situations exist which must be considered: when the device is in read mode interrupts can freely read from the device. interrupts which do not access the device may be used during all the functions. the programmer should also take care when a reset is applied during program or erase operations. the flash will be left in an indeterminate state and data could be lost. limitations of the software the software provided does not implement a full set of the m58bf008's functionality. it is left to the user to implement the program/erase suspend command of the device. the standby mode is a hardware fea- ture of the device and cannot be controlled through software. it is left to the user to implement to asyn- chronous/synchronous read toggle command, this is considered a hardware feature that is outside the scope of these c code drivers. care should be taken in some of the while() loops. no time-outs have been implemented. software ex- ecution may stop in one of the loops due to a hardware error. a /* timeout! */ comment has been put at these places and the user can add a timer to them to prevent the software failing. the software only caters for one device in the system. to add software for more devices a mechanism for selecting the devices will be required. when an error occurs the software simply returns the error message. it is left to the user to decide what to do. either the command can be tried again or, if necessary the device may need to be replaced. conclusion the m58bf008 single voltage flash memory is an ideal product for 32 bit embedded and other computer systems, able to be easily interfaced to microprocessors and driven with simple software drivers written in the c language.
7/27 AN1204 - application note /**** c1204_32.h header file for flash memory ********************************** filename: c1204_32.h description: header file for c1204_32.c v1.02. consult the c file for details copyright (c) 2000 stmicroelectronics. this program is provided as is without warranty of any kind,either expressed or implied, including but not limited to, the implied warranty of merchantability and fitness for a particular purpose. the entire risk as to the quality and performance of the program is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction. *******************************************************************************/ /******************************************************************************* commands for the various functions *******************************************************************************/ /* read signature values */ #define flash_read_manufacturer (-3) #define flash_read_device_code (-2) #define flash_read_version_code (-1) /******************************************************************************* error conditions and return values. see end of c file for explanations and help *******************************************************************************/ #define flash_success (-1) #define flash_block_invalid (-5) #define flash_program_fail (-6) #define flash_offset_out_of_range (-7) #define flash_wrong_type (-8) #define flash_block_failed_erase (-9) #define flash_unprotected (-10) #define flash_protected (-11) #define flash_function_not_supported (-12) #define flash_vpp_invalid (-13) #define flash_erase_fail (-14) #define flash_unprotect_fail (-16) #define flash_protect_fail (-18) #define flash_overlay_fail (-19) #define flash_overlay_disabled (-20) #define flash_overlay_enabled (-21) /******************************************************************************* function prototypes *******************************************************************************/ extern unsigned long flashread( unsigned long uloff ); extern void flashreadreset( void ); extern int flashautoselect( int ifunc ); extern int flashblockerase( unsigned char ucblock ); extern int flashchiperase( int *iresults ); extern int flashprogram( unsigned long uloff, size_t numwords, void *array ); extern int flashoverlayblockdisable( void ); extern int flashoverlayblockenable( void ); extern unsigned long flashoverlayblockread( unsigned long uloff, int *iretval ); extern int flashoverlayblockerase( void ); extern int flashoverlayblockprogram( unsigned long uloff
AN1204 - application note 8/27 , size_t numwords, void *array ); extern char *flasherrorstr( int ierrnum );
9/27 AN1204 - application note /**** c1204_32.c m58bfxxx flash memory driver ********************************* filename: c1204_32.c description: library routines for the m58bfxxx flash memory. version: 1.02 initial release. date: 08/11/2000 author: tim webster, oxford technical solutions (www.ots.ndirect.co.uk) copyright (c) 2000 stmicroelectronics. this program is provided as is without warranty of any kind, either expressed or implied, including but not limited to, the implied warranty of merchantability and fitness for a particular purpose. the entire risk as to the quality and performance of the program is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction. ******************************************************************************** version history. ver. date comments 1.00 03/10/2000 initial creation 1.01 07/11/2000 add programming failure on attempt to program a 0 to a 1 in functions flashprogram and flashoverlayprogram 1.02 08/11/2000 add program verification to functions flashprogram and flashoverlayblockprogram 1.03 11/12/2000 changed base_addr to be long* not int* ******************************************************************************** this source file provides library c code for using the m28wxxxc devices. the following devices are supported in the code: m58bf008 the following functions are available in this library: flashread() to read from the flash flashreadreset() to reset the flash for normal memory access flashautoselect() to get information about the device flashblockerase() to erase a single block flashchiperase() to erase the whole chip flashprogram() to program a double-word or an array flashoverlayblockdisable() to enable main block reads flashoverlayblockenable() to enable overlay block reads flashoverlayblockread() to read a double-word from the overlay block flashoverlayblockerase() to erase the entire overlay block flashoverlayblockprogram() to program a double-word/array to the overlay block flasherrorstr() to return the error string of an error for further information consult the data sheet and the application note. the application note gives information about how to modify this code for a specific application. the hardware specific functions which need to be modified by the user are: flashwrite() for writing a double-word to the flash
AN1204 - application note 10/27 flashread() for reading a double-word from the flash a list of the error conditions is at the end of the code. there are no timeouts implemented in the loops in the code. at each point where an infinite loop is implemented a comment /# timeout! #/ has been placed. it is up to the user to implement these to avoid the code hanging instead of timing out. the source code assumes that the compiler implements the numerical types as unsigned char 8 bits unsigned int 16 bits unsigned long 32 bits additional changes to the code will be necessary if these are not correct. *******************************************************************************/ #include #include c1204_32.h /* header file with global prototypes */ #define use_m58bf008 /******************************************************************************* constants *******************************************************************************/ #define manufacturer_st (0x00000020l) /* st rsig for manufacturer is 0x20 */ #define base_addr ((volatile unsigned long*)0x00000000l) /* base_addr is the base address of the flash, see the functions flashread and flashwrite(). some applications which require a more complicated flashread() or flashwrite() may not use base_addr */ #define cmd_addr (0x00000000l) /* address to write to p/ec */ #ifdef use_m58bf008 #define expected_device (0x000000f0l) /* device code for the m58bf008 */ #define flash_size (0x00040000l) /* total device size */ #define overlay_size (0x00002000l) /* overlay block size */ static const unsigned long blockoffset[] = { 0x00000000l, /* start offset of block 0 */ 0x00002000l, /* start offset of block 1 */ 0x00004000l, /* start offset of block 2 */ 0x00006000l, /* start offset of block 3 */ 0x00008000l, /* start offset of block 4 */ 0x0000a000l, /* start offset of block 5 */ 0x0000c000l, /* start offset of block 6 */ 0x0000e000l, /* start offset of block 7 */ 0x00010000l, /* start offset of block 8 */ 0x00012000l, /* start offset of block 9 */ 0x00014000l, /* start offset of block 10 */ 0x00016000l, /* start offset of block 11 */ 0x00018000l, /* start offset of block 12 */ 0x0001a000l, /* start offset of block 13 */ 0x0001c000l, /* start offset of block 14 */ 0x0001e000l, /* start offset of block 15 */
11/27 AN1204 - application note 0x00020000l, /* start offset of block 16 */ 0x00022000l, /* start offset of block 17 */ 0x00024000l, /* start offset of block 18 */ 0x00026000l, /* start offset of block 19 */ 0x00028000l, /* start offset of block 20 */ 0x0002a000l, /* start offset of block 21 */ 0x0002c000l, /* start offset of block 22 */ 0x0002e000l, /* start offset of block 23 */ 0x00030000l, /* start offset of block 24 */ 0x00032000l, /* start offset of block 25 */ 0x00034000l, /* start offset of block 26 */ 0x00036000l, /* start offset of block 27 */ 0x00038000l, /* start offset of block 28 */ 0x0003a000l, /* start offset of block 29 */ 0x0003c000l, /* start offset of block 30 */ 0x0003e000l /* start offset of block 31 */ }; #endif /* use_m58bf008 */ #define num_blocks (sizeof(blockoffset)/sizeof(blockoffset[0])) /******************************************************************************* static prototypes the following function is only needed in this module. *******************************************************************************/ static unsigned long flashwrite( unsigned long uloff, unsigned long uval ); /******************************************************************************* function: unsigned int flashwrite( unsigned long uloff, unsigned int uval ) arguments: uloff is double-word offset in the flash to write to. uval is the value to be written returns: uval description: this function is used to write a double-word to the flash. on many microprocessor systems a macro can be used instead, increasing the speed of the flash routines. for example: #define flashwrite( uloff, uval ) ( base_addr[uloff] = (unsigned int) uval ) a function is used here instead to allow the user to expand it if necessary. the function is made to return uval so that it is compatible with the macro. pseudo code: step 1: write uval to the double-word offset in the flash step 2: return uval *******************************************************************************/ static unsigned long flashwrite( unsigned long uloff, unsigned long uval ) { /* step1, 2: write uval to the double-word offset in flash and return it */ return base_addr[uloff] = uval; } /******************************************************************************* function: unsigned int flashread( unsigned long uloff ) arguments: uloff is the double-word offset into the flash to read from. returns: the unsigned int at the double-word offset description: this function is used to read a double-word from the flash. on many microprocessor systems a macro can be used instead, increasing the speed of the flash routines. for example:
AN1204 - application note 12/27 #define flashread( uloff ) ( base_addr[uloff] ) a function is used here instead to allow the user to expand it if necessary. pseudo code: step 1: return the value at double-word offset uloff *******************************************************************************/ unsigned long flashread( unsigned long uloff ) { /* step 1 return the value at double-word offset uloff */ return base_addr[uloff]; } /******************************************************************************* function: void flashreadreset( void ) arguments: none return value: none description: this function places the flash in the read array mode described in the data sheet. in this mode the flash can be read as normal memory. all of the other functions leave the flash in the read array mode so this is not strictly necessary. it is provided for completeness. pseudo code: step 1: write command sequence (see instructions table of the data sheet) *******************************************************************************/ void flashreadreset( void ) { /* step 1: write command sequence */ flashwrite( cmd_addr, 0xffffffffl ); } /******************************************************************************* function: int flashautoselect( int ifunc ) arguments: ifunc should be set to one of the read signature values. the header file defines the values for reading the signature. return value: when ifunc is flash_read_manufacturer (-3) the function returns the manufacturers code. the manufacturer code for st is 00000020h. when ifunc is flash_read_device_code (-2) the function returns the device code. the device codes for the parts are: m58bf008 0x000000f0 if ifunc is flash_read_version_code (-1) a value from 0 to 7 is reported to indicate the version of the flash. when ifunc is invalid the function returns flash_function_not_supported (-12) description: this function can be used to read the electronic signature of the device or the manufacturer code. pseudo code: step 1: send the auto select instruction to the device or rsig instruction step 2: read the required function from the device step 3: return the device to read array mode *******************************************************************************/ int flashautoselect( int ifunc ) { int iretval; /* holds the return value */
13/27 AN1204 - application note /* step 1: send the read electronic signature instruction */ flashwrite( cmd_addr, 0x00000090l ); /* step 2: read the required function */ /* note only a0 and a1 are valid read addresses a2-17 dont care */ if( ifunc == flash_read_manufacturer ) iretval = flashread( 0x00000000l ); /* a0 = a1 = 0 */ else if( ifunc == flash_read_device_code ) iretval = flashread( 0x00000001l ); /* a0 = 1, a1 = 0 */ else if( ifunc == flash_read_version_code ) iretval = flashread( 0x00000002l ); /* a0 = 0, a1 = 1 */ else iretval = flash_function_not_supported; /* step 3: return to read array mode */ flashwrite( cmd_addr, 0x000000ffl ); return iretval; } /******************************************************************************* function: int flashblockerase( unsigned char ucblock ) arguments: ucblock is the number of the block to be erased. return value: the function returns the following conditions: flash_success (-1) flash_wrong_type (-8) flash_block_invalid (-5) flash_vpp_invalid (-13) flash_block_failed_erase (-9) description: this function can be used to erase the block specified in ucblock. the function checks that the block is valid before issuing the erase command. once the erase has completed the function checks the status register for errors. any errors are returned, otherwise flash_success is returned. pseudo code: step 1: check for correct flash type step 2: check that the block is valid step 3: issue erase command step 4: wait until program/erase controller is ready step 5: check for any errors step 6: return to read array mode step 7: return error condition *******************************************************************************/ int flashblockerase( unsigned char ucblock ) { int iretval = flash_success; /* holds return value: optimistic initially! */ unsigned long ulstatus; /* holds the status register reads */ /* step 1: check for correct flash type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type;
AN1204 - application note 14/27 /* step 2: check that the block is valid */ if( ucblock >= num_blocks ) return flash_block_invalid; /* step 3: issue erase command */ flashwrite( cmd_addr, 0x00000050l ); /* clear status register */ /* note ! csr also clears b1 bps as well as b3,4 and 5 */ flashwrite( cmd_addr, 0x00000020l ); /* 1st cycle */ flashwrite( blockoffset[ucblock], 0x000000d0l ); /* 2nd cycle */ /* step 4: wait until program/erase controller is ready */ /* timeout! */ do ulstatus = flashread(cmd_addr); while( (ulstatus&0x00000080l) == 0x00000000l ); /* step 5: check for any errors */ if( ulstatus&0x00000008l ) iretval = flash_vpp_invalid; else if( ulstatus&0x00000020l ) iretval = flash_block_failed_erase; /* step 6: return to read array mode */ flashwrite( cmd_addr, 0x00000050l ); /* clear status register */ /* note ! csr also clears b1 bps as well as b3,4 and 5 */ flashwrite( cmd_addr, 0x000000ffl ); /* read array command */ /* step 7: return error condition */ return iretval; } /******************************************************************************* function: int flashchiperase( int *iresults ) arguments: iresults is a pointer to an array where the results will be stored. if iresults == null then no results are stored. return value: the function returns the following conditions: flash_success (-1) flash_wrong_type (-8) flash_erase_fail (-14) if flash_success is returned then results is left unchanged. if flash_erase_fail is returned then results will be filled with the error conditions for each block. the possible error conditions are: flash_success (-1) flash_vpp_invalid (-13) flash_erase_fail (-14) description: the function can be used to erase the whole flash chip. each block is erased in turn. the function only returns when all of the blocks have been erased or have generated an error, except if the flash_vpp_invalid is encountered, in which case the function aborts and reports all remaining blocks as having flash_vpp_invalid. if vpp is invalid for one block then it follows that it will be invalid for subsequent blocks (battery failure?). pseudo code: step 1: check for correct flash type
15/27 AN1204 - application note step 2: for each block step 3: send block erase command step 4: register the errors in the array step 5: if flash_vpp_invalid returned fill rest of results array & abort step 6: return error condition *******************************************************************************/ int flashchiperase( int *iresults ) { unsigned char uccurblock; /* used to track the current block in a range */ int iretval = flash_success; /* return value: initially optimistic */ int ierror; /* holds the latest error */ /* step 1: check for correct flash type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; /* step 2: for each block */ for( uccurblock = 0; uccurblock < num_blocks; uccurblock++ ) { /* step 3: send block erase command */ ierror = flashblockerase( uccurblock ); if( ierror != flash_success ) iretval = flash_erase_fail; /* step 4: register the errors in the array */ if( iresults != null ) iresults[uccurblock] = ierror; /* step 5: if flash_vpp_invalid returned fill rest of results array & abort */ if( ierror == flash_vpp_invalid ) { if( iresults != null ) while( ++uccurblock < num_blocks ) /* on remaining blocks */ iresults[uccurblock] = ierror; /* fill in vpp error */ /* the for() loop will now terminate since uccurblock == num_blocks */ } } /* step 6: return error condition */ return iretval; } /******************************************************************************* function: int flashprogram( unsigned long uloff, size_t numwords, void *array ) arguments: uloff is the double-word offset into the flash to be programmed numwords holds the number of double-words in the array. array is a pointer to the array to be programmed. return value: on success the function returns flash_success (-1). if vpp is invalid then the function returns flash_vpp_invalid (-13) on failure the function returns flash_program_fail (-6). if the address range to be programmed exceeds the address range of the flash device the function returns flash_address_out_of_range (-7) and nothing is programmed. if the wrong type of flash is detected then flash_wrong_type (-8) is returned and nothing is programmed. description: this function is used to program an array into the flash. it does
AN1204 - application note 16/27 not erase the flash first and will fail if the block(s) are not erased first. pseudo code: step 1: check for correct flash type step 2: check the offset range is valid step 3: while there is more to be programmed step 4: check for changes from 0 to 1 step 5: program the next double-word step 6: wait until the program/erase controller is ready step 7: check for any errors step 8: verify program operation step 9: update pointers step 10:clear status register and return to read array mode step 11:return the error condition *******************************************************************************/ int flashprogram( unsigned long uloff, size_t numwords, void *array ) { unsigned long *ularraypointer; /* use an unsigned long to access the array */ unsigned long ullastoff; /* holds the last offset to be programmed */ unsigned long ulstatus; /* holds the status register reads */ int iretval = flash_success; /* return value: initially optimistic */ /* step 1: check that the flash is of the correct type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; /* step 2: check the offset range is valid */ ullastoff = uloff + numwords - 1; if( ullastoff >= flash_size ) return flash_offset_out_of_range; /* step 3: while there is more to be programmed */ ularraypointer = (unsigned long *)array; while( uloff <= ullastoff && iretval == flash_success ) { /* step 4: check for changes from 0 to 1 */ if( ~flashread( uloff ) & *ularraypointer ) return flash_program_fail; /* step 5: program the next double-word */ flashwrite( cmd_addr, 0x00000050l ); /* clear status register */ flashwrite( cmd_addr, 0x00000040l ); /* program set-up */ flashwrite( uloff, *ularraypointer ); /* program value */ /* step 6: wait until program/erase controller is ready */ /* timeout! */ do ulstatus = flashread(cmd_addr); while( (ulstatus&0x00000080l) == 0x00000000l ); /* step 7: check for any errors */ if( ulstatus&0x00000008l ) iretval = flash_vpp_invalid; else if( ulstatus&0x00000010l ) iretval = flash_program_fail; flashwrite( cmd_addr, 0x000000ffl ); /* read array command */
17/27 AN1204 - application note /* step 8: verify program operation */ if ( flashread( uloff ) != *ularraypointer ) { iretval=flash_program_fail; break; } /* step 9: update pointers */ uloff++; /* next double-word offset */ ularraypointer++; /* next double-word in array */ } /* step 10: clear status register and return to read array mode */ flashwrite( cmd_addr, 0x00000050l ); /* clear status register */ flashwrite( cmd_addr, 0x000000ffl ); /* read array command */ /* step 11: return the error condition */ return iretval; } /******************************************************************************* function: int flashoverlayblockdisable( void ); arguments: none return value: on success this function returns flash_success (-1) or if the flash overlay block is already disabled it returns flash_overlay_disabled. if the toggle command was unsuccessful and the flash overlay block will not disable the function returns flash_overlay_fail. description: this function gets the flash into normal block read mode. pseudo code: step 1: check for correct flash type step 2: read status register step 3: if the overlay block is already disabled do nothing and inform the user of that fact step 4: disable the overlay block step 5: read status register step 6: test to see that the disable overlay toggle was successful step 7: return success *******************************************************************************/ int flashoverlayblockdisable( void ) { unsigned long ulstatus; /* used to store status register value */ /* step 1: check that the flash is of the correct type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; /* step 2: read status register */ flashwrite(cmd_addr,0x00000070l); ulstatus = flashread(cmd_addr); /* step 3: test to see is its already disabled */ if (!(ulstatus & 0x00000001l)) { flashwrite( cmd_addr, 0x000000ffl ); return flash_overlay_disabled; }
AN1204 - application note 18/27 /* step 4: disable overlay block */ flashwrite(cmd_addr,0x00000006l); /* step 5: read status register */ flashwrite(cmd_addr,0x00000070l); ulstatus = flashread(cmd_addr); flashwrite( cmd_addr, 0x000000ffl ); /* read array command */ /* step 6: test to see if the operation failed */ if (ulstatus & 0x00000001l) return flash_overlay_fail; /* step 7: return success */ return flash_success; } /******************************************************************************* function: int flashoverlayblockenable( void ); arguments: none return value: on success the function returns flash_success (-1) or if the flash overlay block is already enabled it returns flash_overlay_enabled. if the toggle command is unsuccessful and the flash overlay block will not enable the function returns flash_overlay_fail. description: this function gets the flash into overlay block read mode. pseudo code: step 1: ensure that we have the correct flash step 2: read status register step 3: if the overlay block is already enabled do nothing and inform the user of that fact step 4: enable the overlay block step 5: read status register step 6: test to see that the enable overlay toggle was successful step 7: return success *******************************************************************************/ int flashoverlayblockenable( void ) { unsigned long ulstatus; /* used to store status register value */ /* step 1: check that the flash is of the correct type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; /* step 2: read status register */ flashwrite(cmd_addr,0x00000070l); ulstatus = flashread(cmd_addr); /* step 3: test to see is its already enabled */ if (ulstatus & 0x00000001l) { flashwrite( cmd_addr, 0x000000ffl ); return flash_overlay_enabled; } /* step 4: enable overlay block */ flashwrite(cmd_addr,0x00000006l); /* step 5: read status register */ flashwrite(cmd_addr,0x00000070l);
19/27 AN1204 - application note ulstatus = flashread(cmd_addr); flashwrite( cmd_addr, 0x000000ffl ); /* read array command */ /* step 6: test to see if the operation failed */ if (!(ulstatus & 0x00000001l)) return flash_overlay_fail; /* step 7: return success */ return flash_success; } /******************************************************************************* function: unsigned long flashoverlayblockread( unsigned long uloff, int *iretval); arguments: uloff stores the address offset to read. iretval points to the return value integer. return value: (iretval) a successful read will return flash_success (-1). error conditions possible are: flash_offset_out_of_range (-7) when the address passed to this function is outside the address space of the overlay block. flash_wrong_type (-8) if the part autoselected was not as expected. description: this function automatically enables the overlay block then reads the value at address offset uloff, then resets the flash into normal block read mode. pseudo code: step 1: ensure that the address is a valid overlay block address step 2: enable the overlay block so that it can be read step 3: read the value at the address offset uloff step 4: disable the overlay block return value read *******************************************************************************/ unsigned long flashoverlayblockread( unsigned long uloff, int *iretval ) { unsigned long ultmp; /* used to store read value from overlay block */ /* step 1: is the address valid ? */ if (uloff >= overlay_size) *iretval=flash_offset_out_of_range; else { /* step 2: if the overlay block read enable was not successful return error*/ if (((*iretval=flashoverlayblockenable()) == flash_success) || ( *iretval == flash_overlay_enabled)) { /* step 3: read the value from the overlay block */ ultmp = flashread( uloff ); } else return 0; /* step 4: return the value read from the overlay block */ *iretval=flashoverlayblockdisable(); return ultmp; } return 0; } /******************************************************************************* function: int flashoverlayblockerase( void ); arguments: none return value: this function returns flash_success (-1) if the overlay block has been successfully erased or flash_block_failed_erase (-9) if the operation
AN1204 - application note 20/27 failed. failure return codes are: flash_wrong_type (-8) flash_vpp_invalid (-13) description: this function erases the overlay block. pseudo code: step 1: check for correct flash type step 2: issue the overlay block erase command step 3: wait for the p/e c to complete step 4: check for errors step 5: reset the device in read array mode step 6: return iretval *******************************************************************************/ int flashoverlayblockerase( void ) { int iretval = flash_success; /* holds return value: optimistic initially! */ unsigned long ulstatus; /* holds the status register reads */ /* step 1: check for correct flash type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; /* step 2: issue erase command */ flashwrite( cmd_addr, 0x00000050l ); /* clear status register */ flashwrite( cmd_addr, 0x00000002l ); /* 1st cycle */ flashwrite( blockoffset[0], 0x0000000dl ); /* 2nd cycle */ /* step 3: wait until program/erase controller is ready */ /* timeout! */ do ulstatus = flashread(cmd_addr); while( (ulstatus&0x00000080l) == 0x00000000l ); /* step 4: check for any errors */ if( ulstatus&0x00000008l ) iretval = flash_vpp_invalid; else if( ulstatus&0x00000020l ) iretval = flash_block_failed_erase; /* step 5: return to read array mode */ flashwrite( cmd_addr, 0x00000050l ); /* clear status register */ flashwrite( cmd_addr, 0x000000ffl ); /* read array command */ /* step 6: return error condition */ return iretval; } /******************************************************************************* function: int flashoverlayblockprogram( unsigned long uloff, size_t numwords, void *array ) arguments: uloff is the double-word offset into the flash to be programmed numwords holds the number of double-words in the array. array is a pointer to the array to be programmed. return value: on success the function returns flash_success (-1). if vpp is invalid then the function returns flash_vpp_invalid (-13) on failure the function returns flash_program_fail (-6). if the address range to be programmed exceeds the address range of the flash
21/27 AN1204 - application note device the function returns flash_address_out_of_range (-7) and nothing is programmed. if the wrong type of flash is detected then flash_wrong_type (-8) is returned and nothing is programmed. description: this function is used to program an array into the flash. it does not erase the flash first and will fail if the block(s) are not erased first. pseudo code: step 1: check for correct flash type step 2: check the offset range is valid step 3: enable the overlay block step 4: while there is more to be programmed step 5: check for changes from 0 to 1 step 6: program the next double-word step 7: wait until the program/erase controller is ready step 8: check for any errors step 9: verify program operation step 10:update pointers step 11:disable the overlay block step 12:clear status register and return to read array mode step 13:return the error condition *******************************************************************************/ int flashoverlayblockprogram( unsigned long uloff, size_t numwords, void *array ) { unsigned long *ularraypointer; /* use an unsigned int to access the array */ unsigned long ullastoff; /* holds the last offset to be programmed */ unsigned long ulstatus; /* holds the status register reads */ int iretval = flash_success; /* return value: initially optimistic */ /* step 1: check that the flash is of the correct type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; /* step 2: check the offset range is valid */ ullastoff = uloff + numwords - 1; if( ullastoff >= overlay_size ) return flash_offset_out_of_range; /* step 3: enable the overlay block */ if ((flashoverlayblockenable()) == flash_overlay_fail) return flash_overlay_fail; /* step 4: while there is more to be programmed */ ularraypointer = (unsigned long *)array; while( uloff <= ullastoff && iretval == flash_success ) { /* step 5: check for changes from 0 to 1 */ if( ~flashread( uloff ) & *ularraypointer ) { flashoverlayblockdisable(); return flash_program_fail; } /* step 6: program the next double-word */ flashwrite( cmd_addr, 0x00000050l ); /* clear status register */ flashwrite( cmd_addr, 0x00000004l ); /* program set-up */ flashwrite( uloff, *ularraypointer ); /* program value */
AN1204 - application note 22/27 /* step 7: wait until program/erase controller is ready */ /* timeout! */ do ulstatus = flashread(cmd_addr); while( (ulstatus&0x00000080l) == 0x00000000l ); /* step 8: check for any errors */ if( ulstatus&0x00000008l ) iretval = flash_vpp_invalid; else if( ulstatus&0x00000010l ) iretval = flash_program_fail; flashwrite( cmd_addr, 0x000000ffl ); /* read array command */ /* step 9: verify program operation */ if ( flashread( uloff ) != *ularraypointer ) { iretval=flash_program_fail; break; } /* step 10: update pointers */ uloff++; /* next double-word offset */ ularraypointer++; /* next double-word in array */ } /* step 11: disable the overlay block */ if ((flashoverlayblockdisable()) == flash_overlay_fail) iretval = flash_overlay_fail; /* step 12: clear status register and return to read array mode */ flashwrite( cmd_addr, 0x00000050l ); /* clear status register */ flashwrite( cmd_addr, 0x000000ffl ); /* read array command */ /* step 13: return the error condition */ return iretval; } /******************************************************************************* function: char *flasherrorstr( int ierrnum ); arguments: ierrnum is the error number returned from another flash routine return value: a pointer to a string with the error message description: this function is used to generate a text string describing the error from the flash. call with the return value from another flash routine. pseudo code: step 1: check the error message range. step 2: return the correct string. *******************************************************************************/ char *flasherrorstr( int ierrnum ) { static char *str[] = { flash success, flash poll failure, flash too many blocks, mpu is too slow to erase all the blocks, flash block selected is invalid, flash program failure, flash address offset out of range,
23/27 AN1204 - application note flash is wrong type, flash block failed erase, flash is unprotected, flash is protected, flash function not supported, flash vpp invalid, flash erase fail, flash toggle flow chart failure, flash unprotect failed, flash bank invalid, flash protect failed, flash overlay command failed, flash overlay disabled already, flash overlay enabled already }; /* step 1: check the error message range */ ierrnum = -ierrnum - 1; /* all errors are negative: make +ve & adjust */ if( ierrnum < 0 || ierrnum >= sizeof(str)/sizeof(str[0])) /* check range */ return unknown error\n; /* step 2: return the correct string */ else return str[ierrnum]; } /******************************************************************************* list of errors and return values, explanations and help. ******************************************************************************** return name: flash_success return value: -1 description: this value indicates that the flash command has executed correctly. ******************************************************************************** error name: flash_poll_fail notes: applies to m29 series flash only. this error condition should not occur when using this library. return value: -2 description: the program/erase controller algorithm has not managed to complete the command operation successfully. this may be because the device is damaged solution: try the command again. if it fails a second time then it is likely that the device will need to be replaced. ******************************************************************************** error name: flash_too_many_blocks notes: applies to m29 series flash only. this error condition should not occur when using this library. return value: -3 description: the user has chosen to erase more blocks than the device has. this may be because the array of blocks to erase contains the same block more than once. solutions: check that the program is trying to erase valid blocks. the device will only have num_blocks blocks (defined at the top of the file). also check that the same block has not been added twice or more to the array. ********************************************************************************
AN1204 - application note 24/27 error name: flash_mpu_too_slow notes: applies to m29 series flash only. this error condition should not occur when using this library. return value: -4 description: the mpu has not managed to write all of the selected blocks to the device before the timeout period expired. see block erase command section of the data sheet for details. solutions: if this occurs occasionally then it may be because an interrupt is occuring between writing the blocks to be erased. search for dsi! in the code and disable interrupts during the time critical sections. if this error condition always occurs then it may be time for a faster microprocessor, a better optimising c compiler or, worse still, learn assembly. the immediate solution is to only erase one block at a time. disable the test (by #defineing out the code) and always call the function with one block at a time. ******************************************************************************** error name: flash_block_invalid return value: -5 description: a request for an invalid block has been made. valid blocks number from 0 to num_blocks-1. solution: check that the block is in the valid range. ******************************************************************************** error name: flash_program_fail return value: -6 description: the programmed value has not been programmed correctly. solutions: make sure that the block containing the value was erased before programming. try erasing the block and re-programming the value. if it fails again then the device may have blocks protected. it is also possible for the block to be locked but is unlikely if /wp pin is kept high. if the program fail still results it is likely that the flash is suspect. ******************************************************************************** error name: flash_offset_out_of_range return value: -7 description: the address offset given is out of the range of the device. solution: check the address offset is in the valid range. ******************************************************************************** error name: flash_wrong_type return value: -8 description: the source code has been used to access the wrong type of flash. solutions: use a different flash chip with the target hardware or contact stmicroelectronics for a different source code library. ******************************************************************************** error name: flash_block_failed_erase return value: -9 description: the previous erase to this block has not managed to successfully erase the block. solution: the block may be protected/locked or the flash is suspect. ******************************************************************************** return name: flash_unprotected return value: -10 description: the user has requested to unprotect a block that is already unprotected. this is just a warning to the user that their operation did not make any changes and was not necessary.
25/27 AN1204 - application note ******************************************************************************** error name: flash_protected return value: -11 description: the user has attempted to erase, program or protect a block of the flash that is protected. the operation failed because the block was protected. solutions: choose another (unprotected) block for erasing or programming. alternatively change the block protection status of the current block. (see datasheet for more details). in the case of the user protecting a block that is already protected, this warning notifies the user that the command had no effect. ******************************************************************************** return name: flash_function_not_supported return value: -12 description: the user has attempted to make use of functionality not available on this flash device (and thus not provided by the software drivers). this is simply a warning to the user. ******************************************************************************** error name: flash_vpp_invalid notes: applies to m28 series flash only. return value: -13 description: a program or a block erase has been attempted with the vpp supply voltage outside the allowed ranges. this command had no effect since an invalid vpp has the effect of protecting the whole of the flash device. solution: the (hardware) configuration of vpp will need to be modified to make programming or erasing the device possible. ******************************************************************************** error name: flash_erase_fail return value: -14 description: this indicates that the previous erasure of the whole device has failed. solution: investigate this failure further by attempting to erase each block individually. if erasing a single block still causes failure, then the flash sadly needs replacing. ******************************************************************************** error name: flash_toggle_fail return value: -15 notes: applies to m29 series flash only. this error condition should not occur when using this library. description: the program/erase controller algorithm has not managed to complete the command operation successfully. this may be because the device is damaged. solution: try the command again. if it fails a second time then it is likely that the device will need to be replaced. ******************************************************************************** error name: flash_unprotect_fail return value: -16 description: this error return value indicates that a block unprotect command was unsuccessful. solution: try the command again. if it fails a second time then it is likely that the device is either locked or will need to be replaced. (part is unlocked but protected on power-up with /wp pin at v_high). ********************************************************************************
AN1204 - application note 26/27 error name: flash_bank_invalid return value: -17 notes: this applies to m59drxxx series flash only. description: this error return value indicates that a bank does not exist. ******************************************************************************** error name: flash_protect_fail return value: -18 description: this error return value indicates that a block protect command was unsuccessful. solution: try the command again. if it fails a second time then it is likely that the device is either locked or will need to be replaced. (part is unlocked but protected on power-up with /wp pin at v_high). ******************************************************************************** error name: flash_overlay_fail return value: -19 description: this error indicates that any overlay block associated function has failed. ******************************************************************************** error name: flash_overlay_disabled return value: -20 description: this error informs the user that a overlayblockdisable command was not neccesary because the overlay block was already disabled. ******************************************************************************** error name: flash_overlay_enabled return value: -21 description: this error informs the user that a overlayblockenable command was not neccesary because the overlay block was already enabled. *******************************************************************************/
27/27 AN1204 - application note if you have any questions or suggestion concerning the matters raised in this document please send them to the following electronic mail address: ask.memory@st.com (for general enquiries) please remember to include your name, company, location, telephone number and fax number. information furnished is believed to be accurate and reliable. however, stmicroelectronics assumes no responsibility for the co nsequences of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. no license is granted by implication or otherwise under any patent or patent rights of stmicroelectronics. specifications mentioned in this publicati on are subject to change without notice. this publication supersedes and replaces all information previously supplied. stmicroelectronics prod ucts are not authorized for use as critical components in life support devices or systems without express written approval of stmicroelectro nics. the st logo is registered trademark of stmicroelectronics all other names are the property of their respective owners ? 2000 stmicroelectronics - all rights reserved stmicroelectronics group of companies australia - brazil - china - finland - france - germany - hong kong - india - italy - japan - malaysia - malta - morocco - singapore - spain - sweden - switzerland - united kingdom - u.s.a. www.st.com


▲Up To Search▲   

 
Price & Availability of AN1204

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X